package com.njrsrc.android.app;

import java.util.ArrayList;
import java.util.List;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.njrsrc.R;
import com.njrsrc.android.adapters.JobBoothListAdapter;
import com.njrsrc.android.bean.JobBoothInfo;
import com.njrsrc.android.bean.UserInfo;
import com.njrsrc.android.library.ptr.PullToRefreshBase;
import com.njrsrc.android.library.ptr.PullToRefreshBase.Mode;
import com.njrsrc.android.library.ptr.PullToRefreshBase.OnRefreshListener;
import com.njrsrc.android.library.ptr.PullToRefreshListView;
import com.njrsrc.android.library.vpi.BaseSampleActivity;
import com.njrsrc.android.tasks.JobBoothTask;
import com.njrsrc.android.views.TitleBar;
import com.njrsrc.common.url.RequestUrlProvider;
import com.njrsrc.common.utils.Helpers;
import com.njrsrc.common.utils.MsgID;

/**
 * 招聘会
 * 
 * @author Administrator
 * 
 */
public class JobBoothActivity extends BaseSampleActivity implements OnClickListener {
	private TitleBar mBar;
	private UserInfo userInfo;
	private ProgressDialog progressDialog;
	private PullToRefreshListView jobListView;
	private JobBoothListAdapter adapter;
	private int PAGE_ID = 1;
	/** 每页显示的条数 */
	private static final int COUNT = 10;
	private boolean loadingEnd = false;
	private ArrayList<JobBoothInfo> jobBoothList;
	private String jobFairId;
	private String requestUrl;

	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MsgID.JOB_BOOTH_QUERY_ERROR:
				Helpers.closeProgressDialog(progressDialog);
				Toast.makeText(JobBoothActivity.this, 
						getResources().getString(R.string.connectError), 
						Toast.LENGTH_SHORT).show();
				break;
			case MsgID.JOB_BOOTH_QUERY_FAIL:
				Helpers.closeProgressDialog(progressDialog);
				break;
			case MsgID.JOB_BOOTH_QUERY_SUCCESS:
				Helpers.closeProgressDialog(progressDialog);
				jobListView.onRefreshComplete();
				adapter.setData(jobBoothList);
				adapter.notifyDataSetChanged();
				clear();
				break;

			default:
				break;
			}
			super.handleMessage(msg);
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_job_fair);
		initView();
	}

	private void initView() {
		mBar = (TitleBar) findViewById(R.id.activity_title_bar);
		mBar.setTitleText("展位详情");
		mBar.back.setOnClickListener(this);
		jobListView = (PullToRefreshListView) findViewById(R.id.job_listView);
		jobListView.setScrollingWhileRefreshingEnabled(!jobListView.isScrollingWhileRefreshingEnabled());
		jobListView.setMode(Mode.PULL_FROM_END);
		jobListView.setOnRefreshListener(new OnRefreshListener<ListView>() {
 			@Override
 			public void onRefresh(PullToRefreshBase<ListView> refreshView) {
 				if(!loadingEnd)
 				{
 					initData();
 				}
 			}
 		});
		
		adapter = new JobBoothListAdapter(JobBoothActivity.this);
		jobListView.setAdapter(adapter);
		
		progressDialog = Helpers.showProgressDialog(JobBoothActivity.this, R.string.loading);
		
		Bundle data = getIntent().getExtras();
		jobFairId = data.getString("jobFairId");
		initData();
		
	}
	
	@Override
	protected void onResume() {
		super.onResume();
	}
	
	private void initData()
	{
		List<String> params = new ArrayList<String>();
		params.add(jobFairId);
		params.add(AppApplication.mToken);
		params.add(AppApplication.mUserID);
		params.add("0");
		params.add(PAGE_ID + "");
		params.add(COUNT + "");
		requestUrl = RequestUrlProvider.JOB_FAIR_BOOTH_LIST;
		String url = Helpers.combinaStr(requestUrl, params);
		userInfo = new UserInfo();
		jobBoothList = new ArrayList<JobBoothInfo>();
		Object[] args = new Object[4];
		args[0] = mHandler;
		args[1] = url;
		args[2] = userInfo;
		args[3] = jobBoothList;
		new JobBoothTask().execute(args);
	}
	
	private void clear()
	{
		if(jobBoothList == null || jobBoothList.size() == 0 || jobBoothList.size() < COUNT)
        {
        	loadingEnd = true;
        	jobListView.setMode(Mode.DISABLED);
        }else
        {
        	PAGE_ID++;
        }
		jobBoothList.clear();
		jobBoothList = null;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tb_ibtn_back:
			finish();
			break;
		default:
			break;
		}
	}
}